import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { LoginService } from 'src/app/service/login.service';
import { MessageService } from 'src/app/service/message.service';
import { UserInfoData, UserInfoService } from 'src/app/service/user-info/user-info.service';
import { environment } from 'src/environments/environment';

@Component({
  selector: 'app-account',
  templateUrl: './account.component.html',
  styleUrls: ['./account.component.scss']
})
export class AccountComponent implements OnInit {

  constructor(
    private userInfoService: UserInfoService,
    private router: Router,
    private login: LoginService,
    private message: MessageService
  ) { }

  phone = ''
  userInfo: UserInfoData = {
    userId: '',
    name: '',
    userCode: '',
    phone: '',
    mail: '',
    headPic: '',
    sex: '',
    certifications: '',
    wxNo: '',
    openId: '',
  }
  currentSetting = 0
  updateItem = ''
  updateValue = ''
  ngOnInit(): void {
    this.pageInit()
  }

  pageInit(){
    if(this.userInfoService.getUserInfo()){
      this.userInfo = this.userInfoService.getUserInfo() as UserInfoData
    }else{
      this.router.navigate(['/login'])
    }
  }

  switchSetting(type: number){
    this.currentSetting = type
  }

  setAccount(type: string){
    if(this.userInfo.certifications === '1' && type !== 'mail'){
      this.message.info('已实名认证,此项信息不可修改')
      return
    }
    this.updateItem = type
    this.updateValue = this.userInfo[type]
  }
  closeSetting(){
    this.updateItem = ''
  }
  updateUserInfo(){
    console.log(this.userInfo)
    this.userInfo.phone = ''
    this.login._updateUserInfo(this.userInfo).subscribe({
      next: (res) => {
        this.userInfoService.setUserInfo(res)
        this.userInfo = res
        this.message.success('用户信息更新成功')
        this.closeSetting()
      },
      error: (error) => {
        this.userInfo[this.updateItem] = this.updateValue
        this.closeSetting()
        this.message.error('用户信息更新失败, 请重试')
      }
    })
  }

  setTelModal = false
  setTelLoading = false
  setTel(){
    this.setTelModal = true
  }
  setTelCancel(){
    this.setTelModal = false
  }
  setTelOk(){
    console.log(this.userInfo)
    if(!this.userInfo.phone){
      this.message.error('手机号码不能为空!')
      return
    }

    if(!this.userInfo.verificationCode){
      this.message.error('验证码不能为空!')
      return
    }
    this.setTelLoading = true
    this.login._updateUserInfo(this.userInfo).subscribe({
      next: (res) => {
        this.userInfoService.setUserInfo(res)
        this.userInfo = res
        this.message.success('修改用户绑定手机号码成功')
        this.setTelModal = false
      },
      error: (error) => {
        this.message.error('修改用户绑定手机号码失败,请重试')
        this.setTelLoading = false
      },
      complete: () => {
        console.log('aaa')
        this.setTelLoading = false
      }
    })
  }

  // 密码设置
  resetPwdVisible = false
  setPassword(){ 
    this.resetPwdVisible = true
  }

  //#region 第三方绑定
  bindModalIsVisible = false;
  bindType = '';
  bindCodeSrc = '';
  bindCodeStatus = 0;
  isSpinning = false;
  // 重新获取 二维码
  refreshCode(){
    if(this.bindType === '微信'){
      this.wechatBind();
    }
  }
  // 绑定微信
  wechatBind(){
    this._beforeBind('微信');
    this.login._getWeChatBindCode().subscribe({
      next: res => {
        this._afterGetCode(res, 'wx');
      }, 
      error: error => {
        this.message.createErrorMessage(error);
      }
    })
  }
  
  _beforeBind(type: string){
    this.bindType = type;
    this.bindModalIsVisible = true;
    this.isSpinning = true;
  }
  _afterGetCode(res: any, type: any){
    let key = res.key;
    this.bindCodeSrc = res.qrcodeUrl;
    this.isSpinning = false;
    this.bindCodeStatus = 0;

    this._getIsAuth(key, type);
    
  }

  _getIsAuth(key: any, type: any){
    this.login._getIsAuth(key, type).subscribe({
      next: res => {
        if(res && res.userId){
          this.bindCodeStatus = 1;
        }
      }, 
      error: error => {
        this.bindCodeStatus = 3; // 二维码失效
        this.bindCodeSrc = '';
      }
    })
  }

  bindModalHandleCancel(){
    this.bindModalIsVisible = false;
    this.bindCodeSrc = '';
  }
  bindModalHandleOk(){

  }
  closeModal(){
    this.bindModalIsVisible = false;
    this.getAccountInfo();
  }
  //#endregion 第三方绑定
  getAccountInfo(){
    this.login._getUserInfo().subscribe({
      next: res => {
        if(res){
          this.userInfoService.setUserInfo(res)
          this.userInfo = res
          this.message.success('用户信息更新成功')
        }
      }, 
      error: error => {
        this.message.createErrorMessage(error)
      }
    })
  }

  goAuth(){
    if(this.userInfo.certifications === '1'){
      this.message.info('已实名认证')
      return
    }
    location.href = environment.AUTH_REAL_URL
  }

}
